/*
 * File.cpp
 *
 *  Created on: 22/06/2014
 *      Author: Vitor
 */

#include "File.h"

File::File(std::string file_name, std::string mode) {
	(fp = fopen(file_name.c_str(), mode.c_str())) != nullptr ? file_exists =
																		true :
																file_exists =
																		false;
}

File::~File() {
	fclose(fp);
}

void File::binary_write(const std::vector<char>& data) {
	fwrite(&data[0], sizeof(char), data.size(), fp);
}

std::vector<char> File::binary_read(size_t count) {
	std::vector<char> read_bytes(count);
	fread(&read_bytes[0], sizeof(char), count, fp);
	return read_bytes;
}

std::vector<char> File::read_file_contents() {
	fseek(fp, 0, SEEK_END);
	auto tamanho = ftell(fp);
	rewind(fp);

	return this->binary_read(tamanho);
}
